package com.ruicar.afs.cloud.afscase.channel.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.channel.common.Constants;
import com.ruicar.afs.cloud.afscase.channel.entity.DirectCarDealer;
import com.ruicar.afs.cloud.afscase.channel.mapper.DirectCarDealerMapper;
import com.ruicar.afs.cloud.afscase.channel.service.DirectCarDealerService;
import com.ruicar.afs.cloud.common.core.uid.AfsSequenceGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@SuppressWarnings("AlibabaUndefineMagicConstant")
@Service
public class DirectCarDealerServiceImpl extends ServiceImpl<DirectCarDealerMapper, DirectCarDealer> implements DirectCarDealerService {

    /**
     * 判断返回的序号长度
     */
    static final Integer LENGTH1 = 1;
    static final Integer LENGTH2 = 2;
    static final Integer LENGTH3 = 3;
    static final Integer LENGTH4 = 4;
    static final Integer LENGTH5 = 5;

    /**
     * 序列号
     */
    @Autowired
    private AfsSequenceGenerator afsSequenceGenerator;

    /**
     * 渠道代码自定义生成生成处理
     *
     * @param keyType
     * @param typeIndex
     * @return
     */
    @SuppressWarnings("AlibabaUndefineMagicConstant")
    @Override
    public String channelCodeSequence(String keyType, String typeIndex, String type) {
        String channelCode = "";
        Long lastValue = afsSequenceGenerator.genNext(keyType, typeIndex);
        if (lastValue != null) {
            int length = lastValue.toString().length();
            if (length == LENGTH1) {
                channelCode = "0000" + lastValue;
            } else if (length == LENGTH2) {
                channelCode = "000" + lastValue;
            } else if (length == LENGTH3) {
                channelCode = "00" + lastValue;
            } else if (length == LENGTH4) {
                channelCode = "0" + lastValue;
            } else if (length >= LENGTH5) {
                channelCode = lastValue.toString();
            }
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        String year = sdf.format(date);
        if (Constants.COMMONCARDEALER.equals(type)) {
            return "DPPT" + year + "-" + channelCode;
        }
        return "DPZY" + year + "-" + channelCode;
    }
}
